<template>
  <div>
    <el-card>
      <el-form ref="form" :model="form" label-width="80px" class="form">
        <div style="display: flex">
          <el-form-item label="学科">
            <el-select v-model="form.subject" placeholder="请选择">
              <el-option label="语文" value="语文" />
              <el-option label="数学" value="数学" />
              <el-option label="英语" value="英语" />
              <el-option label="物理" value="物理" />
              <el-option label="生物" value="生物" />
              <el-option label="化学" value="化学" />
              <el-option label="政治" value="政治" />
              <el-option label="历史" value="历史" />
              <el-option label="地理" value="地理" />
            </el-select>
          </el-form-item>
          <el-form-item label="阶段">
            <el-select v-model="form.step" placeholder="请选择">
              <el-option label="初级" value="1" />
              <el-option label="中级" value="2" />
              <el-option label="高级" value="3" />
            </el-select>
          </el-form-item>
          <el-form-item label="企业">
            <el-select v-model="form.enterprise" placeholder="请选择">
              <el-option label="超级管理员" value="超级管理员" />
              <el-option label="管理员" value="管理员" />
              <el-option label="老师" value="老师" />
              <el-option label="学生" value="学生" />
            </el-select>
          </el-form-item>
          <el-form-item label="题型">
            <el-select v-model="form.type" placeholder="请选择">
              <el-option label="单选" value="1" />
              <el-option label="多选" value="2" />
              <el-option label="简答" value="3" />
            </el-select>
          </el-form-item>
        </div>
        <div style="display: flex">
          <el-form-item label="难度">
            <el-select v-model="form.difficulty" placeholder="请选择">
              <el-option label="简单" value="1" />
              <el-option label="一般" value="2" />
              <el-option label="困难" value="3" />
            </el-select>
          </el-form-item>
          <el-form-item label="作者">
            <el-input v-model.trim="form.username" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="form.status" placeholder="请选择">
              <el-option label="禁用" value="0" />
              <el-option label="启用" value="1" />
            </el-select>
          </el-form-item>
          <el-form-item label="日期">
            <el-date-picker
              v-model="form.create_date"
              type="date"
              placeholder="选择日期"
              style="width: 100%"
            />
          </el-form-item>
        </div>
        <div style="display: flex">
          <el-form-item label="标题">
            <el-input v-model.trim="form.title" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search">搜索</el-button>
            <el-button @click="reset">清除</el-button>
            <el-button type="primary" @click="add">+新增试题</el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-card>
    <el-card>
      <el-table :data="questList" border style="width: 100%">
        <el-table-column fixed label="序号" type="index" width="100" />
        <el-table-column prop="title" label="题目" />
        <el-table-column prop="subject,subject_name" label="学科.阶段">
          <template slot-scope="scope">
            {{ scope.row.subject }}.{{ scope.row.subject_name }}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="题型" />
        <el-table-column prop="enterprise_name" label="企业" />
        <el-table-column prop="username" label="创建者" />
        <el-table-column prop="status" label="状态" />
        <el-table-column prop="reads" label="浏览量" />
        <el-table-column fixed="right" label="操作">
          <template v-slot="{ row }">
            <el-button
              type="text"
              size="small"
              @click="edit(row)"
            >编辑</el-button>
            <el-button type="text" size="small" @click="changeStatus(row)">{{
              row.status === '启用' ? '禁用' : '启用'
            }}</el-button>
            <el-button
              type="text"
              size="small"
              @click="del(row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="pageInfo.page"
        :page-size.sync="pageInfo.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total"
        :page-sizes="[1, 5, 10, 20]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <questDialog ref="questDialog" @getQuestList="getQuestList" />
  </div>
</template>

<script>
import { getQuestList, questStatus, delQuest, getQuestDetail } from '../../api/questionBank'
import questDialog from './components/questDialog'
export default {
  name: 'QuestionBankList',
  components: {
    questDialog
  },
  data() {
    return {
      questList: [],
      form: {},
      pageInfo: {
        page: 1,
        limit: 5,
        total: null
      }
    }
  },
  created() {
    this.getQuestList()
  },
  methods: {
    async changeStatus(val) {
      await questStatus(val.id)
      this.getQuestList()
    },
    async getQuestList(val) {
      const res = await getQuestList({
        ...this.pageInfo,
        ...val
      })
      this.pageInfo.total = res.data.pagination.total
      this.questList = res.data.items.map(v => {
        if (v.type === 1) {
          v.type = '单选'
        } else if (v.type === 2) {
          v.type = '多选'
        } else if (v.type === 3) {
          v.type = '简答'
        }
        if (v.status === 1) {
          v.status = '启用'
        } else {
          v.status = '禁用'
        }
        return v
      })
    },
    async search() {
      this.getQuestList(this.form)
    },
    async del(val) {
      const res = await delQuest(val.id)
      console.log(res, 11111)
    },
    add() {
      this.$refs.questDialog.meta = '新增题库测试'
      this.$refs.questDialog.showDialog = true
    },
    async edit(val) {
      const res = await getQuestDetail(val.id)
      console.log(res.data)
      this.$refs.questDialog.meta = '编辑题库测试'
      this.$refs.questDialog.form = res.data
      this.$refs.questDialog.showDialog = true
    },
    reset() {
      this.form = {}
      this.getQuestList()
    },
    handleSizeChange() {
      this.getQuestList(this.form)
    },
    handleCurrentChange() {
      this.getQuestList(this.form)
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep {

  .el-form-item__label {
    font-weight: normal;
  }
  .el-pagination{
    text-align: center;
    margin-top: 20px;
  }
}
</style>
